#include "os_obj.h"
#include "bsp.h"
#include "bsp_dma.h"
#include "systask.h"
#include "bsp_spi_com.h"

uint16_t da_src[10];
uint16_t da_des[10];
uint16_t srx_dex[10];
void dma_tst_call_back(int err);

const pdma_basic_cfg_type dma_cfg =
{
	.req_src = PDMA_MEM,
	.prio = 0,
	.int_en = 1,
	.ctr_sa_mode = PDMA_SAR_INC,
	.ctr_da_mode = PDMA_DAR_INC,
	.ctr_txt_mode = PDMA_REQ_BURST | PDMA_BURST_4,
	.ctr_width = PDMA_WIDTH_16,
	.ctr_amount = 8,
	.sa = (uint32_t)&da_src,
	.da = (uint32_t)&da_des,
	.call_back = dma_tst_call_back
};
static char ch;

void dma_test(void)
{
	if(sys_st.sw_dma_req == 1)
	{
		memset(da_src, 5, sizeof(da_src));
		memset(da_des, 0, sizeof(da_des));
		
		ch = bsp_pdma_alloc();
		
		if(ch >= 0)
		{
			bsp_pdma_cfg(&dma_cfg, ch);
			bsp_pdma_sw_tig(ch);
		}
		
		sys_st.sw_dma_req = 0;
		
		com_spi_init();
		
		com_spi_read((uint8_t*)srx_dex, 12);
	}
	
	if(sys_st.sw_dma_req == 2)
	{
		bsp_pdma_free(ch);
	}
}

void dma_tst_call_back(int err)
{
	NOP1();
	sys_st.sw_dma_req = 2;
}